<?php
class ControllerAffiliatePassword extends FrontController {

  	public function index() {
    	if (!$this->affiliate->isLogged()) {
      		$this->session->data['redirect'] = US('affiliate/password');
      		$this->redirect(US('affiliate/login'));
    	}

    	$this->document->setTitle($this->_['heading_title']);
		$this->document->addScript('static/javascript/jquery/jquery.validate.js');
		
    	if ($this->request->isPost() && $this->validate()) {
			M('affiliate/affiliate');

			$this->model_affiliate_affiliate->editPassword($this->affiliate->getEmail(), $this->request->post['password']);

      		$this->session->set_flashdata('success', $this->_['text_success']);
	  		$this->redirect(US('affiliate/account'));
    	}

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_account'],
			'href'      => US('affiliate/account'),
        	'separator' => $this->_['text_separator']
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			'href'      => US('affiliate/password'),
        	'separator' => $this->_['text_separator']
      	);

		$this->data['password'] = P('password');
   		$this->data['confirm'] = P('confirm');

		$this->display('affiliate/password.tpl');
  	}

  	protected function validate() {
		$pass = true;
    	if ((utf8_strlen($this->request->post['password']) < 4) || (utf8_strlen($this->request->post['password']) > 20)) {
      		$this->setMessage('error_password', $this->_['error_password']);
			$pass = false;
    	}

    	if ($this->request->post['confirm'] != $this->request->post['password']) {
      		$this->setMessage('error_confirm', $this->_['error_confirm']);
			$pass = false;
    	}

	  	return $pass;
  	}
}
?>
